package com.example.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.example.dao.impl.UserDaoImpl;
import com.example.dao2.UserDao;
import com.example.entity.User;

public class LoginServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html"); 
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		//获得打印输出流
		PrintWriter pw=response.getWriter();
		UserDao dao=new UserDaoImpl();
	   //获得客户端请求参数
		String username=request.getParameter("username");
	String	username2 = URLDecoder.decode(username, "UTF-8");
	System.out.println("username2   "+username2);
		String password=request.getParameter("password");
		//登陆返回user实例
		System.out.println("登陆用户名是"+username+" --登陆密码是 "+password);
	    User u=dao.login(username, password);
	    //判断是否登陆成功
	    if (u!=null) {
	    	
	    	pw.print(u.getId());		
	    	System.out.println("登陆成功");
		}else {
			pw.print("0");	
			System.out.println("登陆失败");
		}
	    pw.flush();
	    pw.close();
     }

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
            doGet(request, response);
	}
	//初始化方法
	public void init ()throws ServletException{
		
	}
	//构造方法
	public LoginServlet(){
		super();
	}
	//销毁方法
	public void destory(){
		super.destroy();
	}

}
